package com.example.carbonbackend.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.carbonbackend.model.dto.companyUser.UserGetCompanyRequest;
import com.example.carbonbackend.model.entity.Company;
import com.example.carbonbackend.model.entity.CompanyUser;
import com.example.carbonbackend.service.CompanyService;
import com.example.carbonbackend.service.CompanyUserService;
import com.example.carbonbackend.mapper.CompanyUserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
* @author 25103
* @description 针对表【company_user(公司-人表)】的数据库操作Service实现
* @createDate 2024-04-01 13:27:31
*/
@Service
public class CompanyUserServiceImpl extends ServiceImpl<CompanyUserMapper, CompanyUser>
    implements CompanyUserService{
    @Autowired
    CompanyService companyService;
    @Override
    public QueryWrapper<Company> getCompany(UserGetCompanyRequest request){
        QueryWrapper<CompanyUser> queryWrapper=new QueryWrapper<>();
        QueryWrapper<Company> companyQueryWrapper = new QueryWrapper<>();
        if (request == null) {
            return companyQueryWrapper;
        }
         Long id=request.getId();
         Long companyId=request.getCompanyId();
         Long userId=request.getUserId();
         Date createTime=request.getCreateTime();
         Date updateTime=request.getUpdateTime();
         Integer isDelete=request.getIsDelete();
         Integer pageSize=request.getPageSize();
         Integer current=request.getCurrent();
        String sortField = request.getSortField();
        String sortOrder = request.getSortOrder();

        queryWrapper
                .eq(ObjectUtils.isNotEmpty(id), "id", id)
                .eq("is_delete", false);

        CompanyUser user=getById(userId);
        companyQueryWrapper.eq("id",user.getCompanyId());
// 如果排序字段和排序顺序不为空，则进行排序
        if (StringUtils.isNotBlank(sortField) && StringUtils.isNotBlank(sortOrder)) {
            if ("asc".equalsIgnoreCase(sortOrder)) {
                queryWrapper.orderByAsc(sortField);
            } else if ("desc".equalsIgnoreCase(sortOrder)) {
                queryWrapper.orderByDesc(sortField);
            }
        }


        return companyQueryWrapper;
    }

}




